<template>
    <el-dialog
            :title="!dataForm.collectionId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="数据采集ID" prop="collectionId">
                        <el-input v-model.number="dataForm.collectionId" placeholder="数据采集ID"></el-input>
                </el-form-item>
                <el-form-item label="数据来源系统（如ERP系统名称）" prop="sourceSystem">
                        <el-input v-model="dataForm.sourceSystem" placeholder="数据来源系统（如ERP系统名称）"></el-input>
                </el-form-item>
                <el-form-item label="数据类型（如总账、明细账、凭证等）" prop="dataType">
                        <el-input v-model="dataForm.dataType" placeholder="数据类型（如总账、明细账、凭证等）"></el-input>
                </el-form-item>
                <el-form-item label="采集期间（如会计期间）" prop="collectionPeriod">
                        <el-date-picker
                                v-model="dataForm.collectionPeriod"
                                type="datetime"
                                placeholder="采集期间（如会计期间）">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="采集频率（如每日、每周、每月）" prop="collectionFrequency">
                        <el-input v-model="dataForm.collectionFrequency" placeholder="采集频率（如每日、每周、每月）"></el-input>
                </el-form-item>
                <el-form-item label="采集时间" prop="collectionTime">
                        <el-date-picker
                                v-model="dataForm.collectionTime"
                                type="datetime"
                                placeholder="采集时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="数据文件格式（如Excel、CSV）" prop="fileFormat">
                        <el-input v-model="dataForm.fileFormat" placeholder="数据文件格式（如Excel、CSV）"></el-input>
                </el-form-item>
                <el-form-item label="采集状态（如成功、失败）" prop="status">
                        <el-input v-model="dataForm.status" placeholder="采集状态（如成功、失败）"></el-input>
                </el-form-item>
                <el-form-item label="采集失败时的错误信息" prop="errorMessage">
                        <el-input v-model="dataForm.errorMessage" placeholder="采集失败时的错误信息"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            collectionId: null,
            sourceSystem: null,
            dataType: null,
            collectionPeriod: null,
            collectionFrequency: null,
            collectionTime: null,
            fileFormat: null,
            status: null,
            errorMessage: null
          },
          dataRule: {
            collectionId: [
                        { required: true, message: '数据采集ID不能为空', trigger: 'blur' }
            ],
            sourceSystem: [
                        { required: true, message: '数据来源系统（如ERP系统名称）不能为空', trigger: 'blur' }
            ],
            dataType: [
                        { required: true, message: '数据类型（如总账、明细账、凭证等）不能为空', trigger: 'blur' }
            ],
            collectionPeriod: [
                        { required: true, message: '采集期间（如会计期间）不能为空', trigger: 'blur' }
            ],
            collectionFrequency: [
                        { required: true, message: '采集频率（如每日、每周、每月）不能为空', trigger: 'blur' }
            ],
            collectionTime: [
                        { required: true, message: '采集时间不能为空', trigger: 'blur' }
            ],
            fileFormat: [
                        { required: true, message: '数据文件格式（如Excel、CSV）不能为空', trigger: 'blur' }
            ],
            status: [
                        { required: true, message: '采集状态（如成功、失败）不能为空', trigger: 'blur' }
            ],
            errorMessage: [
                        { required: true, message: '采集失败时的错误信息不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.collectionId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.collectionId = id || 0
            if (this.dataForm.collectionId) {
              this.$http({
                url: this.$http.adornUrl(`/app/data_collection/info/${this.dataForm.collectionId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appdatacollection
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/data_collection/${!this.dataForm.collectionId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>